﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Card : MonoBehaviour
{
    private static GameObject card;
    private static CardItem cardItem;

    CardMember[] cardMembers;
    /// <summary>
    /// 创建卡牌
    /// </summary>
    public static void CreateCard(CardItem _cardItem)
    {
        //生成物体
        card = Instantiate(Resources.Load<GameObject>("Card"), GameObject.Find("Canvas").transform);

        card.transform.Find("MainContentImage").GetComponent<Image>().sprite = Resources.Load(_cardItem.mainSprite, typeof(Sprite)) as Sprite;
        card.transform.Find("TextBgImage").GetChild(0).GetComponent<Text>().text = _cardItem.cardDescription;

        cardItem = _cardItem;
        card.gameObject.AddComponent<Card>();
    }

    private void Start()
    {
        cardMembers = GameObject.Find("CardPanel").transform.GetComponentsInChildren<CardMember>();

        StartCoroutine(Disabled());
    }
    private IEnumerator Disabled()
    {
        //隐藏卡片到右侧卡片框中。
        for (int i = 0; i < cardMembers.Length; i++)
        {
            if (!cardMembers[i].used)
            {
                cardMembers[i].InitCardMember(cardItem, card);
                break;
            }
        }
        //1.5s后隐藏卡片
        yield return new WaitForSeconds(1.5f);

        card.SetActive(false);
    }
}

public class CardItem
{ 
    public int id { set; get; }
    public string mainSprite { set; get; }
    public string cardDescription { set; get; }
    public string boxSprite { set; get; }

    public CardItem(int id, string mainSprite, string _boxSprite, string cardDes)
    {
        this.id = id;
        this.mainSprite = mainSprite;
        this.boxSprite = _boxSprite;
        this.cardDescription = cardDes;
    }
}